# -*- coding:utf-8 -*-
import time
from datetime import datetime
from selenium import webdriver
from selenium.webdriver import ActionChains

from appUtils import WaitSecond, browser_changeTopWindow, browser_doWait, waitInfiniti, getWebDriver

LoginName = "18133623141"
LoginPd = "Hrg123456"


def loadData(webProxyDriver):
    (browser, _, _) = webProxyDriver
    result = [];
    # 登录
    browser.get("http://app.baidu.com/apps/")
    browser_doWait(browser)
    if (browser.current_url == "http://app.baidu.com/"):
        browser.find_element_by_class_name("login").click()
        browser_doWait(browser)
        browser.find_element_by_id("TANGRAM__PSP_10__userName").send_keys(LoginName)
        browser.find_element_by_id("TANGRAM__PSP_10__password").send_keys(LoginPd)
        browser.find_element_by_id("TANGRAM__PSP_10__submit").click()
        browser_doWait(browser)
    # 跳到下载量页面
    # 百度会进行短信验证不知如何处理
    browser.get("http://app.baidu.com/chart/dailyIndex?layout=download&product_id=10277370")
    browser_doWait(browser)
    # 解析数据
    line_chart = browser.find_element_by_css_selector("div[class='echarts line-chart']")
    width = line_chart.size["width"]
    height = line_chart.size["height"]
    count = 31
    itemWidth = width / count
    itemHeight = height / 3
    for item in range(count):
        # 鼠标悬停事件
        ActionChains(browser).move_to_element_with_offset(line_chart,
                                                          xoffset=itemWidth * item,
                                                          yoffset=itemHeight).perform()
        tip = line_chart.find_elements_by_tag_name("div")[1]
        time.sleep(1)
        """
        20190111
        如e定制: 0
        """
        targetText = tip.text;
        # print(targetText)
        if (len(targetText) > 0):
            _date = targetText[:8]
            _timeStr = targetText[targetText.find(":") + 2]
            result.append((formateDate(_date), _timeStr))
    print("baidu", result)
    # waitInfiniti()
    # browser.quit()
    return result


def formateDate(date):
    """
    20190113
    :return:  2018-12-30
    """
    _date = datetime.strptime(date, "%Y%M%d");
    _dateTuple = _date.timetuple();
    return str(_dateTuple.tm_year) + "-" + str(_dateTuple.tm_mon) + "-" + str(_dateTuple.tm_mday)

# print(formateDate("20190111"))
# loadData(False)
